package com.car.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Sa-Token 配置
 *
 * @author Car System
 */
@Configuration
public class SaTokenConfig implements WebMvcConfigurer {

    /**
     * 注册 Sa-Token 拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaInterceptor(handle -> {
            // 登录认证拦截
            SaRouter.match("/**")
                    .notMatch("/auth/login")
                    .notMatch("/doc.html")
                    .notMatch("/webjars/**")
                    .notMatch("/swagger-resources/**")
                    .notMatch("/v3/api-docs/**")
                    .check(r -> StpUtil.checkLogin());

            // 管理员权限认证
            SaRouter.match("/user/**", r -> StpUtil.checkRole("admin"));
        })).addPathPatterns("/**");
    }
}

